/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.SchoolDTO;
import dms.model.School;
import dms.model.util.PageIterator;
import dms.service.SchoolService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author leejohn
 *
 */
public class SchoolAction extends DispatchAction {
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		int[] params = null;
		
		params = PageUtil.parsePageParameter(request);
		int start = params[0];
		int count = params[1];
		PageIterator<School> schools = schoolService.findAllSchool(start, count);
		
		request.setAttribute("schools", schools);
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		return mapping.findForward("addForm");
		
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SchoolDTO dto = (SchoolDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		
		try {
			schoolService.createSchool(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long schoolId = null;
		School school = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");

		try {
			schoolId = Long.valueOf(request.getParameter("id"));
			school = schoolService.findSchoolById(schoolId);
			request.setAttribute("school", school);
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward("updateForm");
		
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SchoolDTO dto = (SchoolDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");

		try {
			schoolService.updateSchool(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
			
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long schoolId = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");

		try {
			schoolId = Long.valueOf(request.getParameter("id"));
			schoolService.deleteSchoolById(schoolId);
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return this.list(mapping, form, request, response);
	}
}
